<%--
  Created by IntelliJ IDEA.
  addUser.jsp
  User: 王威
  Date: 2021/12/2
  Time: 18:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增商品</title>
    <style type="text/css">
        .goodstop{
            text-align: center;
        }
        .goodstop input{
            margin: 0 auto;
            padding: 0 10px;
            width: 300px;
            height: 34px;
            border: 1px solid rgba(255,255,255,.8);
            border-radius: 2px;
            color: #fff;
            background: rgba(0,0,0,.15);
            margin-right: 50px;
        }
        .goodstop select{
            margin: 0 auto;
            padding: 0 10px;
            width: 300px;
            height: 34px;
            border: 1px solid rgba(255,255,255,.8);
            border-radius: 2px;
            color: #fff;
            background: rgba(0,0,0,.15);
            margin-right: 50px;
        }
        button:hover{
            background-color: #0A93CC;
            transform: scale(1.05);
        }
        button {
            transition: all 0.1s linear;
            position: relative;
            left: -50px;
            cursor: pointer;
            height: 50px;
            line-height: 50px;
            width: 150px;
            background-color: #428bca;
            border-color: #357ebd;
            color: #fff;
            -moz-border-radius: 10px;
            -webkit-border-radius: 10px;
            border-radius: 5px;
            -khtml-border-radius: 5px;
            text-align: center;
            vertical-align: middle;
            border: 1px solid transparent;
            font-weight: 900;
            font-size:125%
        }
    </style>
    <script type="text/javascript" src="/js/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        $(function () {
            init();

        })
        function init() {
            gettype();
        }
        function addgoods() {
            $(".tishi").text('')
            var username=$("input[name=userName]").val(); //获取用户名
            var sex = $("input[name=sex]").val();//获取性别
            var nationality = $("input[name=nationality]").val();//获取国家
            var phone = $("input[name=phone]").val();//获取电话
            var brithday = $("input[name=brithday]").val();//获取生日
            var address = $("input[name=address]").val();//获取住址
            var role = $("#type option:selected").val();//获取角色
            var rolename = $("#type option:selected").text();
            if(username==""){
                $(".tishi").text('请输入用户名')
                $("input[name=username]").focus()
            }else if (sex==""){
                $(".tishi").text('请输入性别')
                $("input[name=sex]").focus()
            }else if (nationality==""){
                $(".tishi").text('请输入国家')
                $("input[name=nationality]").focus()
            }else if (phone==""){
                $(".tishi").text('请输入电话号码')
                $("input[name=phone]").focus()
            }else if (brithday==""){
                $(".tishi").text('请输入生日')
                $("input[name=brithday]").focus()
            }else if (address==""){
                $(".tishi").text('请输入住址')
                $("textarea[name=address]").focus()
            }else if(role==0){
                $(".tishi").text('请选择角色')
            }
            else {
                $(".tishi").text('')
                $.ajax({
                    "type":"post",
                    "url":"/usercontrol.do",
                    "data":{"opr":"checkUser","userName":username},
                    "dataType":"json",
                    "success":function(data){
                        if (data.code==200){
                            if (data.data!=null){
                                alert("已存在此账号,请重新输入！")
                                $("input[name='username']").val("").focus();
                            }else {
                                /*不存在账号可添加新账号*/
                                $.ajax({
                                    "type":"post",
                                    "url":"/usercontrol.do",
                                    "data":{"opr":"addUser","userName":username,"sex":sex,"nationality":nationality
                                    ,"date":brithday,"phone":phone,"address":address,"rolename":rolename},
                                    "dataType":"json",
                                    "success":function(data){
                                        if (data.code==200){
                                            if (data.data==true){
                                                alert("添加成功！")
                                                window.location.href = "/user/master.jsp"
                                            }else{
                                                alert("添加失败！")

                                            }
                                        }
                                    }
                                })
                            }
                        }
                    }
                });




                // $.getJSON("/usercontrol.do",params,function (data) {
                //     alert(2)
                //     if (data.code==200){
                //         alert(3)
                //         console.log(data.data)
                //         if (data.data!=null){
                //             $(".tishi").text('用户名已存在，重新输入！')
                //             $("input[name=username]").focus()
                //             clearText();
                //         }else {
                //
                //             params={"opr":"addgoods","type":type,"goods_id":goods_id,"width":width,"height":height,"length":length,"info":info}
                //             $.post("/viewgoods.do",params,function (data) {
                //                 if (data.code==200){
                //                     if (data.data==true){
                //                         $(".tishi").text('添加成功')
                //                     }else {
                //                         $(".tishi").text('添加失败')
                //                     }
                //                 }
                //             },"json")
                //         }
                //     }
                // },"json")
            }
        }
        /*获取所有角色*/
        function gettype() {
            $.post("/usercontrol.do",{"opr":"getallrole"}, function(data) {
                $("#type").html("<option value='0'>请选择角色</option>");
                $(data.data).each(function(i,n){
                    if (n.rid==5){
                        return;
                    }
                    $("#type").append(
                        "<option value=" + n.rid + ">" + n.roleName
                        + "</option>");
                });
            },"json")
        }
        /*清空文本框*/
        function clearText() {
            $('.goodstop input').val('');
        }
    </script>
</head>
<body>
<div class="goodstop" style="border: 1px solid black;width: 100%;height: 680px" >
    <div style="margin-top: 150px;width: auto;height: auto;">
        <h1 style="color: white">新增员工</h1>
        <span style="color: red" class="tishi"></span>
        <table style="margin: auto auto">

            <tr>
                <td>用户名:</td>
                <td>
                    <input type="text" name="userName" placeholder="请输入用户名">
                </td>
            </tr>
            <tr>
                <td>性别:</td>
                <td>
                    <input type="text" maxlength="1" name="sex" placeholder="请输入性别">
                </td>
            </tr>
            <tr>
                <td>国家:</td>
                <td>
                    <input type="text" name="nationality" placeholder="请输入国家">
                </td>
            </tr>
            <tr>
                <td>电话号码:</td>
                <td>
                    <input type="number" name="phone" placeholder="请输入电话号码" maxlength="11">
                </td>
            </tr>
            <tr>
                <td>生日:</td>
                <td>
                    <input type="date" name="brithday" placeholder="请输入你的生日"></input>
                </td>
            </tr>
            <tr>
                <td>家庭地址:</td>
                <td>
                    <input type="text" name="address" placeholder="请输入家庭地址">
                </td>
            </tr>
            <tr>
                <td>角色:</td>
                <td>
                    <select id="type"></select>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <button style="float:left;margin-left: 50px" onclick="addgoods()">提交</button>
                    <button style="margin-left: 50px" onclick="clearText()">清空</button>
                </td>
            </tr>
        </table>
    </div>
</div>
</body>
</html>